-- chat 管理所有的聊天信息
local skynet = require "skynet"
local socket = require "skynet.socket"
local CMD = {}
local SOCKET = {}
local gate
local agent = {}

local userInfos = {}

function CMD.addUser(user)
    local username = user.username
    local password = user.password

    if userInfos[username] == nil then
        userInfos[username] = password
        -- 注册成功
        skynet.ret(skynet.pack({0, "Register success"}))
    else
        -- 已经被注册
        skynet.retpack({1, "Account exist!"})
    end
end

function CMD.login(user)
    local username = user.username
    local password = user.password

    local code;
    local msg;

    if userInfos[username] == nil then
        -- 用户不存在
        code = 1
        msg = 'User does not exist'
    else
        local pwd = userInfos[username]
        if pwd == password then
            -- 登录成功
            code = 0
            msg = 'Logged in'
        else
            -- 密码错误
            code = 2
            msg = 'Wrong password'
        end
    end
    skynet.ret(skynet.pack({code, msg}))
end

function CMD.start(conf)
end

skynet.start(function()
    skynet.dispatch("lua", function(_, _, command, ...)
        print("UserInfo Command:", command)
        local f = CMD[command]
        skynet.ret(skynet.pack(f(...)))
    end)
end)
